package com.youhong.teethcare.services;

import android.app.Application;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

//import com.mob.MobSDK;
import com.tencent.bugly.crashreport.CrashReport;
import com.youhong.teethcare.MainActivity;
import com.youhong.teethcare.javabeans.UserInfo;
import com.youhong.teethcare.receiver.TimerService;
import com.youhong.teethcare.utils.DataReceiverUtils;
import com.youhong.teethcare.utils.LanguageUtils;

import java.util.Calendar;

/**
 * Created by Administrator on 2018/4/9 0009.
 */

public class MyApplication extends Application {

    private ListenerReceiver receiver;

    @Override
    public void onCreate() {

        super.onCreate();
        CrashReport.initCrashReport(getApplicationContext(), "61bbafaa9f", false);
        registerReceiver();
        //CrashReport.testJavaCrash();
        //MobSDK.init(this);

   /*   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            startForegroundService(new Intent(getApplicationContext(),BackgroundRunningHelp.class));

        } else {
            startService(new Intent(getApplicationContext(),BackgroundRunningHelp.class));
        }*/


       // startService(new Intent(getApplicationContext(),BackgroundRunningHelp.class));

        try {
            PackageManager packageManager = getApplicationContext().getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    getApplicationContext().getPackageName(), 0);
            Common.appVersion = packageInfo.versionName;

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        Common.languageUtils = new LanguageUtils(getApplicationContext());
        Common.userInfo = UserInfo.getUserInfoFromSharedPreferences(getApplicationContext());

        DataReceiverUtils.init(this);
       // startService(intent);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            startForegroundService(intent);
//
//        } else {
//            startService(intent);
//       }



        //是否是第一次进入APP，如果是第一次进入APP，就给Common.UserInfo 中的数据做初始化

        if (Common.userInfo.isFirstTimeIntoAPP() == false) {

            Calendar calendar = Calendar.getInstance();
            calendar.set(1990, 00, 01);
            Common.userInfo.setBirthday(calendar);
            Common.userInfo.setNickName("Teethcare");

            //Male
            Common.userInfo.setGender(1);
            Common.userInfo.setHand(0);
            //NONE
            Common.userInfo.setManner(0);
            //健康
            Common.userInfo.setCondition(0);

            //刷牙时长
            Common.userInfo.setBrushTime(120);

            Common.userInfo.setClockSwitch1(false);
            Common.userInfo.setClockSwitch2(false);
            Common.userInfo.setClockSwitch3(false);
            Common.userInfo.setClockSwitch4(false);

            Common.userInfo.setClockTime1("08:30");
            Common.userInfo.setClockTime2("13:00");
            Common.userInfo.setClockTime3("21:00");
            Common.userInfo.setClockTime4("23:00");

            Common.userInfo.setClockDayOfWeek1(0x7F);
            Common.userInfo.setClockDayOfWeek2(0x7F);
            Common.userInfo.setClockDayOfWeek3(0x7F);
            Common.userInfo.setClockDayOfWeek4(0x7F);
            Common.userInfo.setChangeHead(Calendar.getInstance().getTimeInMillis());
            Common.userInfo.setDrinkingNotification(false);

            Common.userInfo.setFirstTimeDisplayPlaque(true);
            //在StartPage 之中调整。
            //Common.userInfo.setFirstTimeIntoAPP(true);
            Common.userInfo.SyncToLocal(getApplicationContext());


        }
    }
    private void registerReceiver() {
        IntentFilter filter = new IntentFilter();

        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        receiver = new ListenerReceiver();
        registerReceiver(receiver, filter);
    }
    private static final String TAG = "MyApplication";
    @Override
    public void onTerminate() {
        super.onTerminate();
    }

    class ListenerReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                        BluetoothAdapter.ERROR);
                switch (state) {
                    case BluetoothAdapter.STATE_ON:
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
//                                final String address = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
//                                if (!TextUtils.isEmpty(address)) {
//                                    BleManager.getInstance().reConnect();
//                                }
                            }
                        }, 600);


                        Log.i(TAG, "onReceive: 开启蓝牙");
                        break;
                    case BluetoothAdapter.STATE_OFF:
                        if(MainActivity.mDevice!=null)
                            MainActivity.mDevice.disconnectDeviceAndClose();
                        Log.i(TAG, "onReceive: 关闭蓝牙");
                        break;

                }
            }
        }
    }

}
